package com.becca.cake.service.statistcs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.becca.cake.core.excel.excelWriter.ExcelInfo;
import com.becca.cake.core.excel.excelWriter.impl.ExcelWriteHandle;
import com.becca.cake.core.exception.ServiceException;
import com.becca.cake.dao.order.OrderDAO;
import com.becca.cake.dao.statistics.StatisticDAO;
import com.becca.cake.dao.system.DataDictionaryDAO;
import com.becca.cake.model.order.OrderModel;
import com.becca.cake.model.statistics.FollowSourceModel;
import com.becca.cake.model.statistics.FollowSourceTypeCountModel;
import com.becca.cake.model.statistics.FusionChartsModel;
import com.becca.cake.model.statistics.OrderRoseModel;
import com.becca.cake.model.statistics.OrderTypeCountModel;
import com.becca.cake.model.statistics.SaleReportModel;
import com.becca.cake.model.statistics.UserConsumptionModel;
import com.becca.cake.service.statistcs.IStatisticsService;
import com.becca.cake.util.DateUtils;

@Service
public class StatisticsServiceImpl implements IStatisticsService {
    @Autowired
    private StatisticDAO      statisticDAO;

    @Autowired
    private DataDictionaryDAO dataDictionaryDAO;

    @Autowired
    public OrderDAO           orderDAO;

    @Override
    public List<OrderModel> selectSatasticOrderGoods(Map<String, Object> map)
                                                                             throws ServiceException {
        String orderStatus = OrderModel.getInvoiceOrderStatus();
        map.put("orderStatus", orderStatus);
        return statisticDAO.selectSatasticOrderGoods(map);
    }

    @Override
    public void exportStatisticsOrder(HttpServletRequest request, HttpServletResponse response,
                                      String deliveryDate) {
        if (StringUtils.isEmpty(deliveryDate)) {
            throw new ServiceException(OrderModel.DELIVERY_DATE_IS_NULL);
        }
        //step1:封装请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("deliveryDate", deliveryDate);
        paramMap.put("orderStatus", "2,3,4,5");
        //step2:查询订单信息
        List list = this.statisticDAO.getExportStatisticsOrder(paramMap);
        ExcelInfo info = new ExcelInfo();
        info.setColumnHeader("订单编号,订单编码,商品名称,商品编码,商品类型,商品尺寸,商品重量,商品单价,商品数量,收货人,电话,地址");
        info.setFeilds("id,orderCode,goodsName,goodsStandardCode,goodsTypeName,goodsSize,goodsWeight,goodsPrice,goodsNumber,consignee,consignPhone,consignAddress");
        info.setDatas(list);
        info.setSheetName("订单信息");
        info.setFileName("订单报表--" + deliveryDate);
        ExcelWriteHandle.getInstance().writeEcxcel(info, request, response);
    }

    @Override
    public List<OrderModel> selectSatasticCouponCode(Map<String, Object> map)
                                                                             throws DataAccessException {
        return this.statisticDAO.selectSatasticCouponCode(map);
    }

    @Override
    public void exportStatisticsCouponCode(HttpServletRequest request,
                                           HttpServletResponse response, String couponCode,
                                           String statisticCouponCodeStartDate,
                                           String statisticCouponCodeEndDate) {
        if (StringUtils.isEmpty(couponCode)) {
            throw new ServiceException(OrderModel.COUPON_CODE_IS_NULL);
        }

        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orderStatus", "5");
        paramMap.put("couponType", "2");
        paramMap.put("couponCode", couponCode);
        paramMap.put("statisticCouponCodeStartDate", statisticCouponCodeStartDate);
        paramMap.put("statisticCouponCodeEndDate", statisticCouponCodeEndDate);
        List list = this.statisticDAO.selectSatasticCouponCode(paramMap);

        ExcelInfo info = new ExcelInfo();
        info.setColumnHeader("订单编号,订单编码,用户编号,用户姓名,订单金额,优惠金额");
        info.setFeilds("id,orderCode,userId,userName,totalAmount,couponFee");
        info.setDatas(list);
        info.setSheetName("优惠码使用信息");
        info.setFileName(DateUtils.getStringToday() + "优惠码使用信息报表");
        ExcelWriteHandle.getInstance().writeEcxcel(info, request, response);

    }

    @Override
    public List<OrderTypeCountModel> selectCountOrderByOrderType(Integer radioDay,
                                                                 String orderPieStartDate,
                                                                 String orderPieEndDate) {
        //step1:封装请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("radioDay", radioDay);
        paramMap.put("orderPieStartDate", orderPieStartDate);
        paramMap.put("orderPieEndDate", orderPieEndDate);
        //step2:查询参数信息
        List<OrderTypeCountModel> listCountModels = this.statisticDAO
            .selectCountOrderByOrderType(paramMap);
        if (listCountModels != null && listCountModels.size() > 0) {
            for (OrderTypeCountModel countModel : listCountModels) {
                countModel.setLabel(OrderModel.getTransOrderType(countModel.getOrderType()));
            }
        }
        return listCountModels;
    }

    @Override
    public Map<String, Object> selectCountOrderAmountByOrderType(Integer radioDay,
                                                                 String orderPieStartDate,
                                                                 String orderPieEndDate) {
        //step1:返回对象信息
        Map<String, Object> returnMap = new HashMap<String, Object>();
        //step2:查询全部订单类别信息
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("radioDay", radioDay);
        paramMap.put("orderPieStartDate", orderPieStartDate);
        paramMap.put("orderPieEndDate", orderPieEndDate);

        //step3:查询全部订单总金额信息
        List<OrderModel> orderModels = this.statisticDAO
            .selectCountOrderAmountByOrderType(paramMap);
        if (orderModels != null && orderModels.size() > 0) {
            List<FusionChartsModel> category = new ArrayList<FusionChartsModel>();//订单类型
            List<FusionChartsModel> discount = new ArrayList<FusionChartsModel>();//实收类型
            List<FusionChartsModel> total = new ArrayList<FusionChartsModel>();//总金额类型
            for (OrderModel orderModel : orderModels) {
                //分类
                FusionChartsModel categoryModel = new FusionChartsModel();
                categoryModel.setLabel(OrderModel.getTransOrderType(orderModel.getOrderType()));
                category.add(categoryModel);
                //实收金额
                FusionChartsModel discountModel = new FusionChartsModel();
                discountModel.setValue(orderModel.getOrderPayPrice());
                discount.add(discountModel);
                //总金额
                FusionChartsModel totalModel = new FusionChartsModel();
                totalModel.setValue(orderModel.getOrderSumPrice());
                total.add(totalModel);
            }
            returnMap.put("category", category);
            returnMap.put("discount", discount);
            returnMap.put("total", total);
        }
        return returnMap;
    }

    @Override
    public List<OrderTypeCountModel> selectCountOrderBySku(Integer radioDay,
                                                           String orderPieStartDate,
                                                           String orderPieEndDate) {
        //step1:封装请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("radioDay", radioDay);
        paramMap.put("orderPieStartDate", orderPieStartDate);
        paramMap.put("orderPieEndDate", orderPieEndDate);
        //step2:查询参数信息
        List<OrderTypeCountModel> listCountModels = this.statisticDAO
            .selectCountOrderBySku(paramMap);
        return listCountModels;
    }

    @Override
    public OrderModel selectOrderReportInfo(Integer radioDay, String orderPieStartDate,
                                            String orderPieEndDate) {
        //step1:封装请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("radioDay", radioDay);
        paramMap.put("orderPieStartDate", orderPieStartDate);
        paramMap.put("orderPieEndDate", orderPieEndDate);
        //step2:查询参数信息
        return this.statisticDAO.selectOrderReportInfo(paramMap);
    }

    @Override
    public Map<String, Object> selectOrderRoseChart(Integer radioDay, String orderStartDate,
                                                    String orderEndDate) {
        //step1:封装请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("radioDay", radioDay);
        paramMap.put("orderPieStartDate", orderStartDate);
        paramMap.put("orderPieEndDate", orderStartDate);
        //step2:查询订单信息
        List<OrderRoseModel> listOrderRose = this.statisticDAO.selectOrderRoseChart(paramMap);
        //step3：封装返回信息
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (listOrderRose != null && listOrderRose.size() > 0) {
            List<OrderTypeCountModel> dayList = new ArrayList<OrderTypeCountModel>();//时间
            List<OrderTypeCountModel> publicOrderList = new ArrayList<OrderTypeCountModel>();//普通订单
            List<OrderTypeCountModel> companyOrderList = new ArrayList<OrderTypeCountModel>();//公司订单
            List<OrderTypeCountModel> eatOrderList = new ArrayList<OrderTypeCountModel>();//试吃订单
            List<OrderTypeCountModel> freeOrderList = new ArrayList<OrderTypeCountModel>();//免费订单
            List<OrderTypeCountModel> b2bOrderList = new ArrayList<OrderTypeCountModel>();//B2B订单
            List<OrderTypeCountModel> olcOrderList = new ArrayList<OrderTypeCountModel>();//olcB订单
            List<OrderTypeCountModel> meiOrderList = new ArrayList<OrderTypeCountModel>();//点评(美团)订单
            List<OrderTypeCountModel> guanjiaOrderList = new ArrayList<OrderTypeCountModel>();//生日管家订单
            List<OrderTypeCountModel> qinziOrderList = new ArrayList<OrderTypeCountModel>();//亲子周末订单
            List<OrderTypeCountModel> sumOrderList = new ArrayList<OrderTypeCountModel>();//总订单
            for (OrderRoseModel roseModel : listOrderRose) {
                //时间
                OrderTypeCountModel dayModel = new OrderTypeCountModel();
                dayModel.setLabel(DateUtils.date2StrDate(roseModel.getDays()));
                dayList.add(dayModel);
                //普通订单
                OrderTypeCountModel publicModel = new OrderTypeCountModel();
                publicModel.setValue(roseModel.getPublicOrder());
                publicOrderList.add(publicModel);
                //公司订单
                OrderTypeCountModel companyModel = new OrderTypeCountModel();
                companyModel.setValue(roseModel.getCompanyOrder());
                companyOrderList.add(companyModel);
                //试吃订单
                OrderTypeCountModel eatModel = new OrderTypeCountModel();
                eatModel.setValue(roseModel.getEatOrder());
                eatOrderList.add(eatModel);
                //免费订单
                OrderTypeCountModel freeModel = new OrderTypeCountModel();
                freeModel.setValue(roseModel.getFreeOrder());
                freeOrderList.add(freeModel);
                //B2B订单
                OrderTypeCountModel b2bModel = new OrderTypeCountModel();
                b2bModel.setValue(roseModel.getB2bOrder());
                b2bOrderList.add(b2bModel);
                //olc订单
                OrderTypeCountModel olcModel = new OrderTypeCountModel();
                olcModel.setValue(roseModel.getOlcOrder());
                olcOrderList.add(olcModel);
                //点评(美团)订单
                OrderTypeCountModel meiModel = new OrderTypeCountModel();
                meiModel.setValue(roseModel.getMeiOrder());
                meiOrderList.add(meiModel);
                //生日管家订单
                OrderTypeCountModel guanjiaModel = new OrderTypeCountModel();
                guanjiaModel.setValue(roseModel.getGuanjiaOrder());
                guanjiaOrderList.add(guanjiaModel);
                //亲子周末订单
                OrderTypeCountModel qinziModel = new OrderTypeCountModel();
                qinziModel.setValue(roseModel.getQinziOrder());
                qinziOrderList.add(qinziModel);
                //总订单
                OrderTypeCountModel sumModel = new OrderTypeCountModel();
                sumModel.setValue(roseModel.getSumOrder());
                sumOrderList.add(sumModel);
            }
            returnMap.put("dayList", dayList);
            returnMap.put("publicOrderList", publicOrderList);
            returnMap.put("companyOrderList", companyOrderList);
            returnMap.put("eatOrderList", eatOrderList);
            returnMap.put("freeOrderList", freeOrderList);
            returnMap.put("b2bOrderList", b2bOrderList);
            returnMap.put("olcOrderList", olcOrderList);
            returnMap.put("meiOrderList", meiOrderList);
            returnMap.put("guanjiaOrderList", guanjiaOrderList);
            returnMap.put("qinziOrderList", qinziOrderList);
            returnMap.put("sumOrderList", sumOrderList);
        }
        return returnMap;
    }

    @Override
    public List<OrderModel> selectAllOrderAddressPoint(String orderTypes) {
        //step1:请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orderStatus", OrderModel.getDayOrderStatus());
        paramMap.put("orderTypes", orderTypes);
        return this.orderDAO.selectAllOrderAddressPoint(paramMap);
    }

    @Override
    public List<SaleReportModel> selectSatasticOrderSale(Map<String, Object> map)
                                                                                 throws ServiceException {

        String orderStatus = OrderModel.getOrdersStatus();
        map.put("orderStatus", orderStatus);
        return statisticDAO.selectOrderSaleReport(map);
    }

    @Override
    public List<UserConsumptionModel> selectUserConsumption(Map<String, Object> map)
                                                                                    throws ServiceException {
        return this.statisticDAO.selectUserConsumption(map);
    }

    @Override
    public long selectUserConsumptionCount(Map<String, Object> map) throws ServiceException {
        return this.statisticDAO.selectUserConsumptionCount(map);
    }

    @Override
    public List<UserConsumptionModel> selectUserConsumptionDetail(Map<String, Object> map)
                                                                                          throws ServiceException {
        return this.statisticDAO.selectUserConsumptionDetail(map);
    }

    @Override
    public long selectUserConsumptionDetailCount(Map<String, Object> map) throws ServiceException {
        return this.statisticDAO.selectUserConsumptionDetailCount(map);
    }

	@Override
	public Map<String, Object> selectFollowSourceChart(Integer radioDay,
			String followSourceStartDate, String followSourceEndDate)
	{
		 //step1:封装请求参数
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("radioDay", radioDay);
        paramMap.put("followSourceStartDate", followSourceStartDate);
        paramMap.put("followSourceEndDate", followSourceEndDate);
        //step2:查询订单信息
        List<FollowSourceModel> followSourceList = this.statisticDAO.selectFollowSourceChart(paramMap);
        //step3：封装返回信息
        Map<String, Object> returnMap = new HashMap<String, Object>();
        if (followSourceList != null && followSourceList.size() > 0) {
            List<FollowSourceTypeCountModel> dayList = new ArrayList<FollowSourceTypeCountModel>();//时间
            List<FollowSourceTypeCountModel> qrsceneList = new ArrayList<FollowSourceTypeCountModel>();//安家DM
            List<FollowSourceTypeCountModel> qrsceneFollowList = new ArrayList<FollowSourceTypeCountModel>();//所有类型
            List<FollowSourceTypeCountModel> warmActivityList = new ArrayList<FollowSourceTypeCountModel>();//暖心活动
            List<FollowSourceTypeCountModel> linQuanList = new ArrayList<FollowSourceTypeCountModel>();//渠道麟泉
            for (FollowSourceModel followSourceModel : followSourceList) {
                //时间
            	FollowSourceTypeCountModel dayModel = new FollowSourceTypeCountModel();
                dayModel.setLabel(DateUtils.date2StrDate(followSourceModel.getDays()));
                dayList.add(dayModel);
                //安家DM
                FollowSourceTypeCountModel qrsceneModel = new FollowSourceTypeCountModel();
                qrsceneModel.setValue(followSourceModel.getAnjiaDM());
                qrsceneList.add(qrsceneModel);
                //暖心活动warmActivity
                FollowSourceTypeCountModel warmActivityModel = new FollowSourceTypeCountModel();
                warmActivityModel.setValue(followSourceModel.getWarmActivity());
                warmActivityList.add(warmActivityModel);
                //渠道麟泉linQuan
                FollowSourceTypeCountModel linQuanModel = new FollowSourceTypeCountModel();
                linQuanModel.setValue(followSourceModel.getLinQuan());
                linQuanList.add(linQuanModel);
                //关注总次数
                FollowSourceTypeCountModel qrsceneFollowModel = new FollowSourceTypeCountModel();
                qrsceneFollowModel.setLabel(followSourceModel.getFollowSource());
                qrsceneFollowModel.setFollowTimes(followSourceModel.getFollowTimes());
                qrsceneFollowModel.setCancelFollowTimes(followSourceModel.getCancelFollowTimes());
                qrsceneFollowList.add(qrsceneFollowModel);
            }
            returnMap.put("dayList", dayList);
            returnMap.put("qrsceneList", qrsceneList);
            returnMap.put("warmActivityList", warmActivityList);
            returnMap.put("linQuanList", linQuanList);
            returnMap.put("qrsceneFollowList", qrsceneFollowList);
        }
        return returnMap;
	}
}
